from django.shortcuts import render_to_response
from django.template import RequestContext


# --
# build_form
# Shorcut for building the form instance of given form class
# > form                class               Form class
# > request             instance            HttpRequest instance
# > *args               special             tuple
# > **kargs             special             dictionary
# < new_form            instance            Form instance
def build_form( Form, _request, GET=False, *args, **kwargs):
    if not GET and 'POST' == _request.method:
        form = Form(_request.POST, _request.FILES, *args, **kwargs)
    elif GET and 'GET' == _request.method:
        form = Form(_request.GET, _request.FILES, *args, **kwargs)
    else:
        form = Form(*args, **kwargs)
    return form


# --
# render_to
# This is Decorator for Django views that sends returned dict to
# render_to_response function. RequestContext always added as context instance.
# If view doesn't return dict then decorator simply returns output.
# -------------------------------------------------------------------------
# Examples:
# 1. Template name in decorator parameters
# @render_to('template.html')
# def foo(request):
#     bar = Bar.object.all()  
#     return {'bar': bar}
# equals to 
# def foo(request):
#     bar = Bar.object.all()  
#     return render_to_response('template.html', 
#                              {'bar': bar}, 
#                              context_instance=RequestContext(request))
# 2. Template name as TEMPLATE item value in return dictionary
# @render_to()
# def foo(request, category):
#     template_name = '%s.html' % category
#     return {'bar': bar, 'TEMPLATE': template_name}
# equals to
# def foo(request, category):
#     template_name = '%s.html' % category
#     return render_to_response(template_name, 
#                              {'bar': bar}, 
#                              context_instance=RequestContext(request)) 
# -------------------------------------------------------------------------
# > template            string          template name 
def render_to( template ):
    def renderer( function ):
        def wrapper( request, *args, **kwargs ):
            output = function( request, *args, **kwargs )
            if not isinstance( output, dict ):
                return output
            tmpl = output.pop( 'TEMPLATE', template )
            return render_to_response( tmpl, output, context_instance \
            = RequestContext( request ) )
        return wrapper
    return renderer
